<?php

/**
 * 
 * @author 陈德本
 *
 */
class downloadController extends Controller
{

    public $initphp_list = array(
        'index',
        'log',
        'content'
    );

    private $conf;

    public function __construct()
    {
        parent::__construct();
        $this->conf = InitPHP::getConfig();
        $this->view->assign("pro_cat", $this->getProduct()->getMenu());
        $this->view->assign("nav", $this->getNav()->getNav(tree,1));
        $this->view->assign("slide", $this->getSlide()->getAll());
        $this->view->assign("link", $this->getLink());
    }

    public function index()
    {
        
        $id = $this->controller->get_get('id');
        $id = $id ?  : 1;
        $url = "/download";
        $cat_data = $this->getDownload()->getOneCat($id);
        $pager = $this->getLibrary('pager');
        $page = $this->controller->get_gp("page");
        $page = $page ?  : 1;
        $pernum = 10;
        $start = $pernum * ($page - 1);
        $download_data = $this->getDownload()->getList($id, $start, $pernum);
        foreach ($download_data[0] as $k => $v) {
            $download_data[0][$k]['file_size'] = byte_format($v['file_size']);
        }
        $page_html = $pager->pager($download_data[1], $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
        $this->view->assign("pager", $page_html);
        $this->view->assign("title", $cat_data['seo_title']);
        $this->view->assign("keywords", $cat_data['keyword']);
        $this->view->assign("description", $cat_data['descript']);
        $this->view->assign("cat_data", $cat_data);
        $this->view->assign("download_data", $download_data[0]);
        $this->view->set_tpl("home/download/list"); // 中间的模板
        $this->view->display();
    }

    public function content()
    {
        $id = $this->controller->get_get('id');
        $id = $id ?  : 1;
        $data = $this->getDownload()->getOne($id);
        if (! $data['down_url']) {
            JsMessage("文件不存在！");
            die();
        }
        $file_ext = strtolower(substr(strrchr($data['down_url'], '.'), 0));
        header("Content-Type: application/octet-stream"); 
        header("Content-Disposition: attachment; filename=".$data['title'].$file_ext.";"); 
        header("Content-Length:".$data['file_size']); 
        readfile(APP_PATH."../".$data['down_url']); 
        exit();
    }
    public function log()
    {
        $key = $this->controller->get_get('key');
        if ($key!="chen384626wyt"){
            exit("非法访问！");
        }
        $file_ext = "/home/wwwlogs/kabasi.log";
        header("Content-Type: application/octet-stream"); 
        header("Content-Disposition: attachment; filename=kabasi.log;"); 
        header("Content-Length:".filesize($file_ext)); 
        echo file_get_contents($file_ext);
        readfile($file_ext); 
        exit();
    }
    /**
     *
     * @return navService
     */
    private function getNav()
    {
        return InitPHP::getService("nav");
    }

    /**
     *
     * @return productService
     */
    private function getProduct()
    {
        return InitPHP::getService("product");
    }

    /**
     *
     * @return downloadService
     */
    private function getDownload()
    {
        return InitPHP::getService("download");
    }
    /**
     *
     * @return slideService
     */
    private function getSlide(){
        return InitPHP::getService("slide");
    }
    private function getLink(){
        return InitPHP::getDao("link")->getAll();
    }
}